﻿#include <cmath>
#include <QDebug>
#include <QFontMetrics>
#include "element.h"

// 直线标记两边小线段的长度
const double BAR_LEN = 20.0;

DrawLine::DrawLine(void)
    : m_start(QPoint(0, 0)),
      m_end(QPoint(0, 0))
{
    m_drawType = LINE;
    m_state = DS_IDLE;
}

void DrawLine::setStartPoint(QPoint point)
{
    m_start = point;
    m_end = point;
    m_line1_start = point;
    m_line1_end = point;
    m_line2_start = point;
    m_line2_end = point;
    m_length = 0.0;
    m_cos_theta  = 1.0;
    m_sin_theta = 0.0;

    m_state = DS_STATE1;
}

void DrawLine::setTempEndPoint(QPoint point)
{
    m_end = point;
    compute();
}

void DrawLine::setEndPoint(QPoint point)
{
    m_end = point;
    compute();
    updateText();
    //computeBox();
    m_state = Element::DS_FINISH;
    int X = (m_start.x() + m_end.x()) / 2;
    int Y = (m_start.y() + m_end.y()) / 2;
    m_textBox.moveTo(X, Y);

}
void DrawLine::build(QPoint start, QPoint end)
{
    setStartPoint(start);
    setEndPoint(end);
}

void DrawLine::compute(void)
{
    m_length = hypot( m_end.x() - m_start.x(), m_end.y() - m_start.y() );
    if( m_end.x() == m_start.x() ) // 也包括了  m_length == 0 的情况，下面就不需要特殊处理了
    {
        m_cos_theta = 0.0;
        m_sin_theta  = 1.0;
    }
    else
    {
        m_cos_theta = ( m_end.x() - m_start.x() ) / m_length;
        m_sin_theta = ( m_end.y() - m_start.y() ) / m_length;
    }

    double cos_theta = - m_sin_theta; // 与主直线垂直
    double sin_theta = m_cos_theta;

    m_line1_start.setX( m_start.x() - BAR_LEN / 2 * cos_theta );
    m_line1_start.setY( m_start.y() - BAR_LEN / 2 * sin_theta );

    m_line1_end.setX( m_start.x() + BAR_LEN / 2 * cos_theta );
    m_line1_end.setY( m_start.y() + BAR_LEN / 2 * sin_theta );

    m_line2_start.setX( m_end.x() - BAR_LEN / 2 * cos_theta );
    m_line2_start.setY( m_end.y() - BAR_LEN / 2 * sin_theta );

    m_line2_end.setX( m_end.x() + BAR_LEN / 2 * cos_theta );
    m_line2_end.setY( m_end.y() + BAR_LEN / 2 * sin_theta );
}

int DrawLine::mouseMove(QPoint point)
{
    if( m_state == DS_STATE1 )
    {
        setTempEndPoint(point);
        return 1;
    }
    return 0;
}

int DrawLine::mouseLButtonPress(QPoint point)
{
    if( m_state == DS_IDLE )
    {
        setStartPoint(point);
        return 1;
    }
    return 0;
}

int DrawLine::mouseLButtonRelease(QPoint point)
{
    if( m_state == DS_STATE1 )
    {
        setEndPoint(point);
        if( m_start == m_end )
        {
            m_state = Element::DS_ERROR;
        }
        return 1;
    }
    return 0;
}

void DrawLine::computeBox(void)
{
    int min_x, max_x, min_y, max_y;
    min_x = qMin(m_start.x(), m_end.x());
    max_x = qMax(m_start.x(), m_end.x());
    min_y = qMin(m_start.y(), m_end.y());
    max_y = qMax(m_start.y(), m_end.y());
    m_box.setCoords ( min_x, min_y, max_x, max_y );
}

inline void DrawLine::computeLength(void)
{
    m_length = hypot( m_end.x() - m_start.x(), m_end.y() - m_start.y() );
}

void DrawLine::updateText(void)
{
    if(m_length * m_scale >= 1000.0)
    {
        m_text = QString::fromUtf8("L = %1 mm").arg(m_length * m_scale / 1000.0, 2, 'f', 2);
    }
    else
    {
        m_text = QString::fromUtf8("L = %1 um").arg(m_length * m_scale, 2, 'f', 2);
    }
    QFontMetrics fm(m_textFont);
    int textWidth = fm.width(m_text);
    int textHeight = fm.height();
    m_textBox.setWidth(textWidth);
    m_textBox.setHeight(textHeight);

}

void DrawLine::draw( QPainter &painter )
{
    if( m_state != DS_IDLE )
    {
        painter.save();
        //painter.setFont(m_textFont);
        painter.setPen(m_linePen);

        painter.drawLine ( m_start, m_end - QPoint(1, 1) );
        painter.drawLine ( m_line1_start, m_line1_end );
        painter.drawLine ( m_line2_start, m_line2_end );

        if(m_state == Element::DS_FINISH)
        {
            painter.setFont(m_textFont);
            painter.drawText(m_textBox, m_text);
        }
        painter.restore();
    }
}


